define(['jquery','mylayer'], function ($,mylayer) {
	var app={
		//登录框事件初始化
		init:function(){
			var _this=this;
			//显示登录
			$(".show_login").on("click",function(){
				$(".sign_panel .login_panel").show().siblings('.panel').hide();
				_this.showLogin('');
			});
			//显示注册
			$(".show_reg").on("click",function(){
				$(".sign_panel .reg_panel").show().siblings('.panel').hide();
				_this.initlayer_login('');
			});
		},
		initEvent:function(){
			var _this=this;
			$(".login_btn").off("click").on("click",function(){
				$(".sign_panel .login_panel").show().siblings('.panel').hide();
			});
			//显示注册
			$(".signup_btn").off("click").on("click",function(){
				$(".sign_panel .reg_panel").show().siblings('.panel').hide();
			});
			//显示忘记密码
			$(".forget_password").off("click").on("click",function(){
				$(".forgot_tip").html("").hide();
				$(".sign_panel .forget_panel").show().siblings('.panel').hide();
			});
			$(".doLogin").off("click").on("click",function(){
				var form=$(this).closest(".login_form");
				var login_obj=form.find(".login_user");
				var login_user=$.trim(login_obj.val());
				var flag=true;
				if(login_user==""){
					login_obj.next(".error_msg").html(function(){
						return $(this).attr("data-requied");
					}).show();
					flag=false;
				}
				else{
					login_obj.next(".error_msg").html("").hide();
				}
				var pwd_obj=form.find(".login_password");
				var login_password=$.trim(pwd_obj.val());
				if(login_password==""){
					pwd_obj.next(".error_msg").html(function(){
						return $(this).attr("data-requied");
					}).show();
					flag=false;
				}
				else{
					pwd_obj.next(".error_msg").html("").hide();
				}
				if(flag){
					var sub_btn=$(".doLogin");
					if(sub_btn.hasClass('disabled')){
						return false;
					}
					sub_btn.addClass('disabled');
					var data={
						"login_user":login_user,
						"login_password":login_password
					}
					$.postAjax("/UserInfo/login",data,function(result){
						var url=window.location.href;
						if(result.code=="0x00000"){
							if(_this.fevent){
								if(typeof _this.fevent=="function"){
									_this.fevent();
								}
							}
							if(typeof _this.hideLogin=="function"){
								_this.hideLogin();
							}
							var user_name=result.user_name;
							if(url.indexOf("/user/entry")>0){
								window.location.href="/u/"+user_name;
								return false;
							}
							var user_text=result.user_name.length<=10?result.user_name:result.user_name.substring(0,10)+"...";
							var html='<ul class="account clearfix">'+
									'<li class=""><span>'+$("#system_msg").attr("data-welcome")+' , </span><a  title="'+user_name+'" href="/u/'+user_name+'">'+user_text+'</a>'+
									'<ul>'+
									'<li><a class="" href="/u/'+user_name+'/wish">'+$("#system_msg").attr("data-wish")+'</a></li>'+
									'<li><a class="" href="/u/'+user_name+'/praise">'+$("#system_msg").attr("data-praiseup")+'</a></li>'+
									'<li><a class="" href="/u/'+user_name+'/review">'+$("#system_msg").attr("data-review")+'</a></li>'+
									'<li><a class="" href="/user/setting">'+$("#system_msg").attr("data-setting")+'</a></li>'+
									'<li><a class="" href="/user/logout">'+$("#system_msg").attr("data-logout")+'</a></li>'+
									'</ul></li>'+
									'</ul>';
							$(".account_user").html(html);
						}
						else{
							mylayer.showMessage("error",result.msg);
						}
						sub_btn.removeClass('disabled');
					},function(){
						sub_btn.removeClass('disabled');
						mylayer.showMessage("error",$("#system_msg").attr("data-network"));
					});
				}
			});
			$(".doReg").off("click").on("click",function(){
				var form=$(this).closest(".reg_form");
				var flag=true;
				var reg_username_obj=form.find(".reg_username");
				var reg_username=$.trim(reg_username_obj.val());
				if(reg_username==""){
					reg_username_obj.next(".error_msg").html(function(){
						return $(this).attr("data-requied");
					}).show();
					flag=false;
				}
				else{
					reg_username_obj.next(".error_msg").html("").hide();
				}
				var register_email_obj=form.find(".reg_email");
				var register_email=$.trim(register_email_obj.val());
				if(register_email==""){
					register_email_obj.next(".error_msg").html(function(){
						return $(this).attr("data-requied");
					}).show();
					flag=false;
				}
				else if(!/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(register_email)){
					register_email_obj.next(".error_msg").html(function(){
						return $(this).attr("data-valid");
					}).show();
					return false;
				}
				else{
					register_email_obj.next(".error_msg").html("").hide();
				}
				var reg_password_obj=form.find(".reg_password");
				var reg_password=$.trim(reg_password_obj.val());
				if(reg_password==""){
					reg_password_obj.next(".error_msg").html(function(){
						return $(this).attr("data-requied");
					}).show();
					flag=false;
				}
				else{
					reg_password_obj.next(".error_msg").html("").hide();
				}
				if(flag){
					var sub_btn=$(this);
					if(sub_btn.hasClass('disabled')){
						return false;
					}
					sub_btn.addClass('disabled');
					var data={
						"reg_username":reg_username,
						"reg_useremail":register_email,
						"reg_password":reg_password
					};
					$.postAjax("/UserInfo/register",data,function(result){
						sub_btn.removeClass('disabled');
						if(result.code == '0x00000'){
							window.location.href=window.location.href;
						}
						else{
							var obj;
							for(i in result.error_info){
								obj = $(form).find("."+i);
								obj.next(".error_msg").html(result.error_info[i]).show();
							}	
						}
					},function(){
						sub_btn.removeClass('disabled');
						mylayer.showMessage("error",$("#system_msg").attr("data-network"));
					});
				}
			});
			$(".doForget").off("click").on("click",function(){
				var flag=true;
				var form=$(this).closest(".forget_form");
				var forget_email_obj=form.find(".forget_email");
				var forget_email=$.trim(forget_email_obj.val());
				if(forget_email==""){
					forget_email_obj.next(".error_msg").html(function(){
						return $(this).attr("data-requied");
					}).show();
					flag=false;
				}
				else if(!/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(forget_email)){
					forget_email_obj.next(".error_msg").html(function(){
						return $(this).attr("data-valid");
					}).show();
					flag=false;
				}
				else{
					forget_email_obj.next(".error_msg").html("").hide();
				}
				if(flag){
					var sub_btn=$(this);
					if(sub_btn.hasClass('disabled')){
						return false;
					}
					sub_btn.addClass('disabled');
					$(".forgot_tip").html("").hide();
					$.postAjax("/userinfo/reset",{'email':forget_email},function(result){
						sub_btn.removeClass('disabled');
						if(result.msg){
							$(".forgot_tip").html(result.msg).show();
						}
					},function(){
						sub_btn.removeClass('disabled');
						mylayer.showMessage("error",$("#system_msg").attr("data-network"));
					});
				}
			});
		},
		//设置登录完成回调
		initSuccess:function(fevent){
			this.fevent=fevent;
		},
		//显示登录
		showLogin:function(){
			$(".layer_login").show();
			$(".overlay_login").show();
			$(".login_panel").show().siblings('.panel').hide();
			this.initlayer_login();
		},
		//显示忘记密码
		showForget:function(){
			$(".layer_login").show();
			$(".overlay_login").show();
			$(".forget_panel").show().siblings('.panel').hide();
			this.initlayer_login();
		},
		initlayer_login:function(title){
			var _this=this;
			var lay=mylayer.init({
				title:'',
				content:$("#sign_panel_box").html(),
				success:function(){
					_this.initEvent();
				},
				closeEvent:function(){
					
				},
				class_name:"layer_sign_login"
			});
			_this.hideLogin=function(){
				mylayer.closeLayer(lay);
				if($(".mylayer").size()==0){
					$("html,body").css("overflow-y","auto");
				}
				this.fevent="";
			}
		},
		update_menu:function(url){
			$(".u_menu_post").attr("href","/user/"+url);
			$(".u_menu_album").attr("href","/user/"+url+"/album");
			$(".u_menu_wish").attr("href","/user/"+url+"/wish");
			$(".account_url").attr("href","/user/"+url);
		}
	};
	if(typeof app!=undefined){
		$(function(){
			app.init();
		});
	}
	return app;
});



